package com.udp.nml;

import java.io.IOException;
import java.net.InetSocketAddress;

import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.DatagramSessionConfig;
import org.apache.mina.transport.socket.nio.NioDatagramAcceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class UDPServer 
{
	static Logger logger = LoggerFactory.getLogger(UDPServer.class);
	
	public static int PORT = 44777;
	
	public static void main(String[] args)
	{
		logger.info(">>> main");
		
		UDPServer monitorService = new UDPServer();
		monitorService.setup();
		
		logger.info("<<< main");
	}
	
	public void setup()
	{
		logger.info(">>> setup");
		
		try 
		{
			NioDatagramAcceptor acceptor = new NioDatagramAcceptor();
			
			UDPHandler udpHandler = new UDPHandler();
			
			acceptor.setHandler(udpHandler);
	
			DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
			
			chain.addLast("logger", new LoggingFilter());
	
			DatagramSessionConfig dcfg = acceptor.getSessionConfig();
			
			dcfg.setReuseAddress(true);
			
			acceptor.bind(new InetSocketAddress(PORT));
		
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
		
		logger.info("<<< setup");
	}
}
